package com.ssy.lingxi.purchase.controller.tender;

import cn.hutool.core.bean.BeanUtil;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.purchase.api.dto.request.tender.InviteTenderQueryRequest;
import com.ssy.lingxi.purchase.api.dto.request.tender.PlatformInviteTenderQueryRequest;
import com.ssy.lingxi.purchase.api.dto.request.tender.PlatformSubmitTenderQueryRequest;
import com.ssy.lingxi.purchase.api.dto.request.tender.SubmitTenderQueryRequest;
import com.ssy.lingxi.purchase.api.dto.response.tender.InviteTenderDetailResponse;
import com.ssy.lingxi.purchase.api.dto.response.tender.InviteTenderMemberResponse;
import com.ssy.lingxi.purchase.api.dto.response.tender.InviteTenderResponse;
import com.ssy.lingxi.purchase.api.dto.response.tender.SubmitTenderInviteTenderMemberDetailResponse;
import com.ssy.lingxi.purchase.api.enums.tender.InviteTenderInStatusEnum;
import com.ssy.lingxi.purchase.api.enums.tender.InviteTenderOutStatusEnum;
import com.ssy.lingxi.purchase.api.enums.tender.SubmitTenderInStatusEnum;
import com.ssy.lingxi.purchase.api.enums.tender.SubmitTenderOutStatusEnum;
import com.ssy.lingxi.purchase.entity.tender.*;
import com.ssy.lingxi.purchase.service.tender.IInviteTenderService;
import com.ssy.lingxi.purchase.service.tender.ITemplateService;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @menu: 招投标-平台管理
 * 招标管理类
 * @author jw.chen
 * @version 2.0.0
 * @date 2021/3/1
 */
@RestController
@RequestMapping("/purchase/inviteTender/platform")
public class PlatformTenderController extends BaseController {

    @Resource
    private IInviteTenderService inviteTenderService;

    @Resource
    private ITemplateService templateService;

    /**
     * 待审核招标查询列表 22.1.20版本去掉
     * @param
     * @return
     */
  /*  @RequestMapping(value = "/getCheckInviteTenderList", method = RequestMethod.POST)
    public Wrapper<PageData<InviteTenderResponse>> getCheckInviteTenderList(@RequestBody PlatformInviteTenderQueryRequest platformInviteTenderQueryRequest) {
        InviteTenderQueryRequest inviteTenderQueryRequest = BeanUtil.copyProperties(platformInviteTenderQueryRequest, InviteTenderQueryRequest.class);

        List<Integer> inviteTenderOutStatusList = new ArrayList<>();
        inviteTenderOutStatusList.add(InviteTenderOutStatusEnum.Platform_Not_Check_Invite_Tender.getCode());
        inviteTenderQueryRequest.setInviteTenderOutStatusList(inviteTenderOutStatusList);
        List<Integer> inviteTenderInStatusList = new ArrayList<>();
        inviteTenderInStatusList.add(InviteTenderInStatusEnum.Submitted_Invite_Tender.getCode());
        inviteTenderQueryRequest.setInviteTenderInStatusList(inviteTenderInStatusList);

        Page<InviteTender> result = inviteTenderService.getInviteTenderList(null, inviteTenderQueryRequest);
        List<InviteTender> inviteTenderList = result.getContent();
        List<InviteTenderResponse> resultList = inviteTenderList.stream().map(inviteTender -> {
            InviteTenderResponse inviteTenderResponse = BeanUtil.copyProperties(inviteTender, InviteTenderResponse.class);
            inviteTenderResponse.setInviteTenderInStatusValue(InviteTenderInStatusEnum.getMessage(inviteTender.getInviteTenderInStatus()));
            inviteTenderResponse.setInviteTenderOutStatusValue(InviteTenderOutStatusEnum.getMessage(inviteTender.getInviteTenderOutStatus()));
            return inviteTenderResponse;
        }).collect(Collectors.toList());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }*/

    /**
     * 招标查询列表
     */
    @RequestMapping(value = "/getInviteTenderList", method = RequestMethod.POST)
    public Wrapper<PageData<InviteTenderResponse>> getInviteTenderList(@RequestBody PlatformInviteTenderQueryRequest platformInviteTenderQueryRequest) {
        InviteTenderQueryRequest inviteTenderQueryRequest = BeanUtil.copyProperties(platformInviteTenderQueryRequest, InviteTenderQueryRequest.class);
        Page<InviteTender> result = inviteTenderService.getInviteTenderList(null, inviteTenderQueryRequest);
        List<InviteTender> inviteTenderList = result.getContent();
        List<InviteTenderResponse> resultList = inviteTenderList.stream().map(inviteTender -> {
            InviteTenderResponse inviteTenderResponse = BeanUtil.copyProperties(inviteTender, InviteTenderResponse.class);
            inviteTenderResponse.setInviteTenderInStatusValue(InviteTenderInStatusEnum.getMessage(inviteTender.getInviteTenderInStatus()));
            inviteTenderResponse.setInviteTenderOutStatusValue(InviteTenderOutStatusEnum.getMessage(inviteTender.getInviteTenderOutStatus()));
            return inviteTenderResponse;
        }).collect(Collectors.toList());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 查询招标信息
     * @param id 招标id
     */
    @RequestMapping(value = "/getInviteTender", method = RequestMethod.GET)
    public Wrapper<InviteTenderDetailResponse> getInviteTender(@RequestParam Long id) {
        InviteTender inviteTender = inviteTenderService.getInviteTender(id);
        if(inviteTender != null){
            InviteTenderDetailResponse inviteTenderResponse = BeanUtil.copyProperties(inviteTender, InviteTenderDetailResponse.class);
            inviteTenderResponse.setInviteTenderInStatusValue(InviteTenderInStatusEnum.getMessage(inviteTender.getInviteTenderInStatus()));
            inviteTenderResponse.setInviteTenderOutStatusValue(InviteTenderOutStatusEnum.getMessage(inviteTender.getInviteTenderOutStatus()));

            //查询评标模板名称
            Long templateId = inviteTender.getTemplateId();
            if(templateId != null && templateId > 0){
                Template template = templateService.getTemplate(templateId);
                if(template != null){
                    inviteTenderResponse.setTemplateName(template.getName());
                }
            }
            return Wrapper.success(inviteTenderResponse);
        }else{
            throw new BusinessException(ResponseCode.PURCHASE_INVITE_TENDER_NOT_EXIST);
        }
    }

    /**
     * 查询投标列表
     */
    @RequestMapping(value = "/getSubmitTenderList", method = RequestMethod.POST)
    public Wrapper<PageData<InviteTenderMemberResponse>> getSubmitTenderList(@RequestBody PlatformSubmitTenderQueryRequest platformSubmitTenderQueryRequest) {
        SubmitTenderQueryRequest submitTenderQueryRequest = BeanUtil.copyProperties(platformSubmitTenderQueryRequest, SubmitTenderQueryRequest.class);
        Page<InviteTenderMember> result = inviteTenderService.getSubmitTenderList(null, submitTenderQueryRequest);
        List<InviteTenderMember> inviteTenderMemberList = result.getContent();
        List<InviteTenderMemberResponse> resultList = inviteTenderMemberList.stream().map(inviteTenderMember -> {
            InviteTenderMemberResponse inviteTenderMemberResponse = BeanUtil.copyProperties(inviteTenderMember, InviteTenderMemberResponse.class);
            inviteTenderMemberResponse.setSubmitTenderInStatusValue(SubmitTenderInStatusEnum.getMessage(inviteTenderMember.getSubmitTenderInStatus()));
            inviteTenderMemberResponse.setSubmitTenderOutStatusValue(SubmitTenderOutStatusEnum.getMessage(inviteTenderMember.getSubmitTenderOutStatus()));
            return inviteTenderMemberResponse;
        }).collect(Collectors.toList());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 查询投标信息
     * @param id 投标id
     */
    @RequestMapping(value = "/getSubmitTender", method = RequestMethod.GET)
    public Wrapper<SubmitTenderInviteTenderMemberDetailResponse> getSubmitTender(@RequestParam Long id) {
        InviteTenderMember inviteTenderMember = inviteTenderService.getSubmitTenderMember(id);
        if(inviteTenderMember != null){
            SubmitTenderInviteTenderMemberDetailResponse inviteTenderMemberDetailResponse = BeanUtil.copyProperties(inviteTenderMember, SubmitTenderInviteTenderMemberDetailResponse.class);
            inviteTenderMemberDetailResponse.setSubmitTenderInStatusValue(SubmitTenderInStatusEnum.getMessage(inviteTenderMember.getSubmitTenderInStatus()));
            inviteTenderMemberDetailResponse.setSubmitTenderOutStatusValue(SubmitTenderOutStatusEnum.getMessage(inviteTenderMember.getSubmitTenderOutStatus()));

            //四舍五入，保留两位小数
            DecimalFormat decimalFormat = new DecimalFormat("#.00");
            //中标金额
            double winnerAmount = 0;
            double allWinnerAmount = 0;
            Boolean isWin = inviteTenderMember.getIsWin();
            if(isWin != null && isWin){
                SubmitTender submitTender = inviteTenderMember.getSubmitTender();
                if(submitTender != null){
                    List<SubmitTenderMateriel> submitTenderMaterielList = submitTender.getSubmitTenderMateriel();
                    if(submitTenderMaterielList != null && submitTenderMaterielList.size() > 0){
                        for (SubmitTenderMateriel submitTenderMateriel : submitTenderMaterielList) {
                            Boolean isAwardTender = submitTenderMateriel.getIsAwardTender();
                            if (isAwardTender != null && isAwardTender) {
                                Double price = submitTenderMateriel.getPrice();
                                Double awardTenderRatio = submitTenderMateriel.getAwardTenderRatio();
                                Double count = submitTenderMateriel.getInviteTenderMateriel().getCount();
                                winnerAmount = winnerAmount + (price * awardTenderRatio * count / 100);
                                allWinnerAmount = allWinnerAmount + (price * count);
                            }
                        }
                    }
                }
            }

            inviteTenderMemberDetailResponse.setWinnerAmount(Double.parseDouble(decimalFormat.format(winnerAmount)));
            inviteTenderMemberDetailResponse.setAllWinnerAmount(Double.parseDouble(decimalFormat.format(allWinnerAmount)));

            return Wrapper.success(inviteTenderMemberDetailResponse);
        }else{
            throw new BusinessException(ResponseCode.PURCHASE_SUBMIT_TENDER_NOT_EXIST);
        }
    }

    /**
     * 平台审核招标 22.1.20版本去掉
     * @param checkRequest 审核
     * @return
     */
  /*  @RequestMapping(value = "/platformCheckInviteTender", method = RequestMethod.POST)
    public Wrapper<String> platformCheckInviteTender(@RequestBody CheckRequest checkRequest) {
        return Wrapper.success(inviteTenderService.platformCheckInviteTender(checkRequest));
    }*/

    /**
     * 平台审核招标(批量审核通过) 22.1.20版本去掉
     * @param simpleIdListRequest 审核
     * @return
     */
  /*  @RequestMapping(value = "/platformCheckInviteTenderBatch", method = RequestMethod.POST)
    public Wrapper<String> platformCheckInviteTenderBatch(@RequestBody SimpleIdListRequest simpleIdListRequest) {
        return Wrapper.success(inviteTenderService.platformCheckInviteTenderBatch(simpleIdListRequest));
    }*/

}
